package com.zhilianbao.leyaogo.view.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.bql.convenientlog.CLog;
import com.bql.utils.CheckUtils;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.constants.IntentConfig;
import com.zhilianbao.leyaogo.model.response.Coupon.CanUseCard;
import com.zhilianbao.leyaogo.ui.adapter.shoppingcart.CardCanUseAdapter;
import com.zhilianbao.leyaogo.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: hcy
 * Description:可用卡券
 * Created: 2016/11/30 10:33
 */
public class CardDialog extends BaseCouponDialog {

    private List<CanUseCard> mList;
    private CardCanUseAdapter mAdapter;
    private String checkId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mList = getArguments().getParcelableArrayList(IntentConfig.COUPON_DATA);
        checkId = getArguments().getString(IntentConfig.CARD_ID_DATA, null);
        if (!CheckUtils.isNull(checkId) && checkId.length() > 0) {
            List<CanUseCard> list = new ArrayList<>();
            list.addAll(mList);
            String[] strArr = checkId.split(",");
            for (int i = 0; i < strArr.length; i++) {
                for (CanUseCard card : list) {
                    if (card.getCardItemId() == Long.parseLong(strArr[i])) {
                        card.setCheck(true);
                        list.remove(card);
                        break;
                    }
                }
            }
            for (CanUseCard card : list) {
                card.setCheck(false);
            }
        } else {
            for (CanUseCard card : mList) {
                card.setCheck(false);
            }
        }
    }

    @Override
    void setData() {
        mCenterTv.setText(getString(R.string.pay_cart));
        mAdapter = new CardCanUseAdapter(getActivity(), mList);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void OnConfirmClick() {
        super.OnConfirmClick();
//        Set<Integer> set = mAdapter.getCardIds();
        StringBuilder stringBuilder = new StringBuilder();
//        Iterator<Integer> iterator = set.iterator();
        double amount = 0.00;
        int count = 0;
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).isCheck()) {
                stringBuilder.append(mList.get(i).getCardItemId()).append(",");
                amount = Utils.calculatePrice(amount, mList.get(i).getCardBalance(), 1);
                count++;
            }
        }
//        while (iterator.hasNext()) {
//            CanUseCard card = mList.get(iterator.next());
//            stringBuilder.append(card.getCardItemId()).append(",");
//            amount = Utils.calculatePrice(amount,card.getCardBalance(),1);
//            CLog.i("hcy","card.getCardItemId()="+card.getCardItemId());
//        }

        if (mOnConfirmClickListener != null) {
            mOnConfirmClickListener.onConfirmClick(stringBuilder.toString(), amount, count);
        }
        CLog.i("hcy", stringBuilder.toString());
        dismiss();
    }

    @Override
    String getHeaderText() {
        return getString(R.string.use_more_card);
    }

    public void setOnConfirmClickListener(OnConfirmClickListener listener) {
        mOnConfirmClickListener = listener;
    }

    public interface OnConfirmClickListener {
        void onConfirmClick(String str, double amount, int size);
    }

    private OnConfirmClickListener mOnConfirmClickListener;

}
